package cmd

import (
	"context"
	"devops-super/internal/controller/alertLog"
	"devops-super/internal/controller/alertOperate"
	"devops-super/internal/controller/alertPeople"
	"devops-super/internal/controller/alertPeopleMsg"
	"devops-super/internal/controller/alertScene"
	"devops-super/internal/controller/clusterConfig"
	"devops-super/internal/controller/clusterServer"
	"devops-super/internal/controller/dept"
	"devops-super/internal/controller/deptConfig"
	"devops-super/internal/controller/deptServer"
	"devops-super/internal/controller/device"
	"devops-super/internal/controller/deviceConfig"
	"devops-super/internal/controller/deviceGroup"
	"devops-super/internal/controller/deviceGroupConfig"
	"devops-super/internal/controller/distributeFiles"
	"devops-super/internal/controller/distributeTables"
	"devops-super/internal/controller/file"
	"devops-super/internal/controller/firmware"
	"devops-super/internal/controller/iotdata"
	"devops-super/internal/controller/loginlog"
	"devops-super/internal/controller/oplog"
	"devops-super/internal/controller/permission"
	"devops-super/internal/controller/product"
	"devops-super/internal/controller/productConfig"
	"devops-super/internal/controller/productDeptConfig"
	"devops-super/internal/controller/public"
	"devops-super/internal/controller/role"
	"devops-super/internal/controller/sysConfig"
	"devops-super/internal/controller/user"
	"devops-super/internal/controller/userConfig"
	"devops-super/internal/service"
	"devops-super/utility/upload"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gtime"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			SetTimezone(ctx)
			s := g.Server()
			s.Group("/api/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareCORS)
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Bind(
					public.NewV1(),
				)

				// 权限控制路由
				group.Group("/", func(group *ghttp.RouterGroup) {
					group.Middleware(service.Middleware().Auth)

					//后台操作日志记录
					group.Hook("/*", ghttp.HookAfterOutput, service.Oplog().OperationLog)

					group.Bind(
						user.NewV1(),
						permission.NewV1(),
						role.NewV1(),
						dept.NewV1(),
						product.NewV1(),
						device.NewV1(),
						deviceGroup.NewV1(),
						firmware.NewV1(),
						file.NewV1(),
						clusterConfig.NewV1(),
						deptConfig.NewV1(),
						deviceConfig.NewV1(),
						deviceGroupConfig.NewV1(),
						productConfig.NewV1(),
						productDeptConfig.NewV1(),
						sysConfig.NewV1(),
						userConfig.NewV1(),
						clusterServer.NewV1(),
						distributeFiles.NewV1(),
						distributeTables.NewV1(),
						deptServer.NewV1(),
						iotdata.NewV1(),
						oplog.NewV1(),
						loginlog.NewV1(),
						alertLog.NewV1(),
						alertPeople.NewV1(),
						alertOperate.NewV1(),
						alertPeopleMsg.NewV1(),
						alertScene.NewV1(),
					)
				})
			})

			// 初始化
			if err = service.Context().Init(ctx); err != nil {
				g.Log().Fatal(ctx, err)
			}

			register()

			s.Run()
			return nil
		},
	}
)

// 注册相关组件
func register() {
	service.Mycache().CacheSetAdapter()
	upload.Register()
}

func SetTimezone(ctx context.Context) {
	err := gtime.SetTimeZone("Asia/Shanghai")
	if err != nil {
		panic(err)
	}
}

func sysInit(ctx context.Context) {
	// 检查数据库default 有没有配置，如果没有配置，则需要更新数据库配置，并且查出集群相关信息进行更新
}
